<extend name="Public:template" />
<block name="page_body">
<!-- 商品列表页面  -->
<include file="Public:header_productsearch"/>
<div class="productList" id="productListView">
</div>
<div class='morePageBtn' style="display:none"><a href="#" id="morePageBtn"  >显示更多商品</a></div>
<form method="get" action="{:U('Product/list')}" name="form_search" id="form_search">
	<input type="hidden" name="name"   value="{$search.name}"/>
	<input type="hidden" name="cate_code" value="{$search.cate_code}" />
</form>
</block>
<block name="script_footer">
<!-- 商品显示模板  -->
<script type="text/javascript" src="__ROOT__/statics/mobile/scripts/template.min.js"></script>
<script id="productTmpl" type="text/html">
<dl>
	<dt>
		<a href="#" onclick="return false;">
			<img class="lazy"  src="<%=pic%>"/>
		</a>
	</dt>
	<dd>
		<a href="#">
			<h4><%=name%> <%=spec%></h4>
			<p class="promTags"></p>
			<p class="price">
				<span class="now">¥<%=price_real%></span>
				<span class="old">¥<%=price_sale%></span>
			</p>
		</a>
		<span class="cartItemNum"><em id="itemNum<%=prod_id%>"></em></span>
		<a class="cartBtn"  href="javascript:addToCart('<%=prod_id%>','1','1');" ></a>
	</dd>
</dl>
</script>
<script type="text/javascript">
$(document).ready(function(){
	searchProduct();
	//输入搜索条件自动查询数据
	$(".morePageBtn").on( "click", function(event, ui) {
		searchProduct();
		return false;
	});
})
function searchProduct(){
	if($("#form_search").attr('action')!=''){
		$.ajax({
	        'url': $("#form_search").attr('action'),
	        'dataType': "json",
	        'data': $('#form_search').serialize(),
	        'success': function (data) {
	        	var list = data['list'];
	        	var page = data['page'];
	        	$.each(list,function(i,item){
	        		var productItemHtml = template.render('productTmpl', item);
	        		$(productItemHtml).appendTo("#productListView");
	        	});
	        	$("#form_search").attr('action',page['downPageUrl'])
	        	if(page['totalPages']==page['nowPage']){
	        		$(".morePageBtn").hide();
	        	}else{
	        		$(".morePageBtn").show();
	        	}
	        }
	    });
	}

}
function addToCart(prod_id,num,edittype){
	//问题 直接在底部不加载数据了
	//document.location.href = "/cart/doadd.html?prod_id="+prod_id+"&num="+num+"&edittype="+edittype;
	$.ajax({
		'async': false, 
        'url': "/cart/doadd.html",
        'dataType': "json",
        'data': {'prod_id':prod_id,'num':num,'edittype':edittype},
        'success': function (data) {
        	if(data['error']=='0'){
            	var cart_num = data['cart_num'];
            	var item_num = data['item_num'];
            	$("#cartNum").html(cart_num);
            	$("#itemNum"+prod_id).html("×"+item_num);
            	if(data['message']!=""){
            		alert(data['message']);
            	}
        	}else{
        		alert(data['message']);
        	}
        }
    });
}
</script>
</block>